package com.ethink.order.facadeImpl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ethink.order.api.PurchaseOrderFacade;
import com.ethink.order.api.info.PurchaseOrderDetailInfo;
import com.ethink.order.api.info.PurchaseOrderInfo;
import com.ethink.order.api.info.PurchaseOrderSaleInfo;
import com.ethink.order.api.model.*;
import com.ethink.order.api.request.*;
import com.ethink.order.api.response.OverseasDeleveryPreQueryResponse;
import com.ethink.order.api.response.PurchaseOrderDetailQueryResponse;
import com.ethink.order.api.response.TransferPurchaseResponse;
import com.ethink.order.manager.OaPayManager;
import com.ethink.order.manager.PurchaseManager;
import com.ethink.order.service.PurchaseOrderDetailService;
import com.ethink.order.service.PurchaseOrderService;
import com.ethink.order.service.SaleDemandOrderDetailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 采购单 Facade接口实现
 *
 * @author default
 * @date 2022-10-31
 */
@RestController
@Slf4j
@RequestMapping("order-service/facade/v1/purchaseOrder")
public class PurchaseOrderFacadeImpl implements PurchaseOrderFacade {

    @Autowired
    private PurchaseOrderDetailService purchaseOrderDetailService;

    @Autowired
    private PurchaseManager purchaseManager;

    @Autowired
    private PurchaseOrderService purchaseOrderService;

    @Autowired
    private SaleDemandOrderDetailService saleDemandOrderDetailService;

    @Autowired
    private OaPayManager oaPayManager;

    @Override
    public List<PurchaseOrderDetailInfo> queryPurchaseDetailOrderByDetailOrderNo(String detailOrderNo) {
        return purchaseOrderDetailService.queryPurchaseDetailOrderByDetailOrderNo(detailOrderNo);
    }

    @Override
    public void purchaseQuote(PurchaseQuoteRequest request) {
        purchaseOrderDetailService.purchaseQuote(request);
    }

    @Override
    public TransferPurchaseResponse transfer(TransferPurchaseOrderRequest request) {
        return purchaseManager.transferPurchaseOrder(request);
    }

    @Override
    public void add(PurchaseOrderAddRequest request) {
        purchaseManager.add(request);
    }

    @Override
    public List<PurchaseOrderDetailModel> queryPrePurchaseOrder(PrePurchaseOrderQueryRequest request) {
        return purchaseOrderDetailService.queryPrePurchaseOrder(request);
    }

    @Override
    public Page<PurchaseOrderDetailModel> queryPurchaseDetail(PurchaseOrderDetailPageQueryRequest request) {
        return purchaseOrderDetailService.pageQuery(request);
    }

    @Override
    public Integer purchaseDetailUpdate(PurchaseOrderDetailUpdateRequest request) {
        return purchaseOrderDetailService.update(request);
    }

    @Override
    public Page<PurchaseOrderModel> purchaseOrderPageQuery(PurchaseOrderPageQueryRequest request) {
        return purchaseOrderService.purchaseOrderPageQuery(request);
    }

    @Override
    public void purchaseDetailDelete(Long id) {
        purchaseOrderDetailService.purchaseDetailDelete(id);
    }

    @Override
    public PurchaseOrderDetailQueryResponse queryPurchaseOrderDetail(Long id) {
        return purchaseOrderService.queryPurchaseOrderDetail(id);
    }

    @Override
    public Integer purchaseOrderRefine(PurchaseOrderRefineRequest refineRequest) {
        return purchaseOrderService.purchaseOrderRefine(refineRequest);
    }

    @Override
    public Integer purchaseOrderUpdate(PurchaseOrderUpdateRequest updateRequest) {
        return purchaseManager.purchaseOrderUpdate(updateRequest);
    }

    @Override
    public Integer purchaseOrderVoid(Long id) {
        return purchaseManager.purchaseOrderVoid(id);
    }

    @Override
    public OverseasDeleveryPreQueryResponse overseasDeliveryPreQuery(Long id) {
        return purchaseOrderService.overseasDeliveryPreQuery(id);
    }

    @Override
    public void updateStatus(PurchaseOrderUpdateStatusRequest updateStatusRequest) {
        purchaseOrderService.updateStatus(updateStatusRequest);
    }

    @Override
    public List<PurchaseQuoteQueryModel> purchaseQuoteQuery(String detailOrderNo, String createUserCode) {
        return purchaseOrderDetailService.purchaseQuoteQuery(detailOrderNo,createUserCode);
    }

    @Override
    public Integer purchaseWarehouseUpdate(PurchaseWarehouseUpdateRequest request) {
        return purchaseOrderService.purchaseWarehouseUpdate(request);
    }

    @Override
    public Integer updateLockOrderQty(LockOrderQtyUpdateRequest request) {
        return purchaseOrderDetailService.updateLockOrderQty(request);
    }

    @Override
    public PurchaseOrderInfo getPurchaseOrderInfo(String orderNo) {
        return purchaseOrderService.getPurchaseOrderInfo(orderNo);
    }

    @Override
    public PurchaseOrderDetailInfo queryDetailInfoByDetailId(Long id) {
        return purchaseOrderDetailService.queryDetailInfoByDetailId(id);
    }

    @Override
    public PurchaseOrderInfo queryMainById(Long id) {
        return purchaseOrderService.queryMainById(id);
    }

    @Override
    public List<PurchaseOrderNoAndQtyModel> queryPurchaseQty(List<String> orderNos) {
        return purchaseOrderDetailService.queryPurchaseQty(orderNos);
    }

    @Override
    public List<PurchaseOrderSaleInfo> listPurchaseOrderDetailSaleDemandOrderNo(List<String> purchaseDetailOrderNoList) {
        if (CollUtil.isEmpty(purchaseDetailOrderNoList)){
            return CollUtil.newArrayList();
        }
        return saleDemandOrderDetailService.queryExceptSalePriceTaxLocal(purchaseDetailOrderNoList);
    }

    @Override
    public void insertOa(Long id) {
        PurchaseOrderDetailQueryResponse response = purchaseOrderService.queryPurchaseOrderDetail(id);
        oaPayManager.insert(response.getMain(), response.getItem());
    }

    /**
     * 采购订单货品模式分页查询
     *
     * @param request
     * @return
     */
    @Override
    public Page<PurchaseOrderModeModel> purchaseOrderPageModeQuery(PurchaseOrderPageQueryModeRequest request) {
        return purchaseOrderService.purchaseOrderPageModeQuery(request);
    }
}